﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Web
{
    public partial class dang_nhap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["username"] != null)
                {
                    HttpCookie c = Request.Cookies["username"];
                    txtTaiKhoan.Text = c.Value;
                }
            }
        }

        private bool checklogin(string taikhoan, string matkhau)
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from KhachHang where TenDangNhap_KH = @TenDangNhap_KH and MatKhau_KH = @MatKhau_KH";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@TenDangNhap_KH", taikhoan);
            cmd.Parameters.AddWithValue("@MatKhau_KH", matkhau);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                txtTaiKhoan.Text = taikhoan;
                txtMatKhau.Text = matkhau;
                reader.Close();
                conn.Close();
                return true;
            }
            else
            {
                reader.Close();
                conn.Close();
                return false;
            }
        }

        private void dangnhap(string taikhoan, string matkhau)
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from KhachHang where TenDangNhap_KH = @TenDangNhap_KH";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@TenDangNhap_KH", taikhoan);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                if((string)reader["MatKhau_KH"] == matkhau)
                {
                    txtTaiKhoan.Text = taikhoan;
                    txtMatKhau.Text = matkhau;
                    Session["username"] = txtTaiKhoan.Text;
                HttpCookie user = new HttpCookie("username", txtTaiKhoan.Text);
                user.Expires = DateTime.Now.AddDays(1d);
                Response.Cookies.Add(user);
                Response.Redirect("trang-chu.aspx");
                    reader.Close();
                    conn.Close();
                }
                else
                {
                    reader.Close();
                    conn.Close();
                    LblThongBao.Text="Mật khẩu không đúng";
                }
            }
            else
            {
                reader.Close();
                conn.Close();
                LblThongBao.Text = "Tên đăng nhập không đúng";
            }
        }

        protected void DangNhapButton_Click(object sender, EventArgs e)
        {
            dangnhap(txtTaiKhoan.Text, txtMatKhau.Text);
        }
    }
}